package net.zjitc.service.Category.impl;

import net.zjitc.damain.category.Category;
import net.zjitc.dao.Category.CategoryDao;
import net.zjitc.service.Category.CategoryException;
import net.zjitc.service.Category.CategoryService;
import net.zjitc.service.CategorySecond.CategorySecondService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2017/3/13.
 */
@Service
@Transactional
public class CategoryServiceImpl implements CategoryService {
    @Resource
    private CategoryDao categoryDao;
    @Resource
    private CategorySecondService categorySecondService;

    @Override
    public List<Category> findAll() {
        return categoryDao.findAll();
    }

    @Override
    public Category findById(Integer cid) {
        return categoryDao.findById(cid);
    }

    @Override
    public void Delete(Category category) throws CategoryException {
        List cslist = categorySecondService.findByCid(category.getCid());
        if (cslist.size()>0){
            throw new CategoryException("一级分类下有二级分类，不能删除");
        }
        categoryDao.Delete(category);
    }

    @Override
    public void add(Category category) {
        categoryDao.add(category);
    }
}
